﻿///***************************************************************
/// 시  스  템 : Updms.Fit
/// 단위시스템 : FIT
/// 계      층 : Presentation Layer
/// 프로그램명 : Fit2013r
/// 파  일  명 : Fit2013r.aspx.cs
/// 설      명 : 월별대비분석표
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2014-06-26 송시명   최초 작성
///***************************************************************
using System;
using System.Collections;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.AppFramework.Web.UI.CommandBar;
using Base.Dac;

using Updms.Fit.Exe;

namespace Updms.Fit
{
	public partial class Fit2013r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		public static readonly string PID = "Fit2013r";
		public static readonly string gs_path_info = "/updms/Fit/" + PID + ".aspx";

		Global global = new Global();
		
		//검색
		protected string kw1 = "";	//전월
		protected string kw2 = "";	//당월

		//메뉴 선택값
		protected string gs_sm = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SetEnabledMonthBtn();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetBaseYm();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
		}

		#endregion

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			kw1 = ddl_search_pre_year.SelectedValue + "-" + ddl_search_pre_month.SelectedValue;
			kw2 = ddl_search_this_year.SelectedValue + "-" + ddl_search_this_month.SelectedValue;
		}

		#endregion

		#region 마스터 Method (SetBaseYm)

		//기준년월 설정
		private void SetBaseYm()
		{
			int li_curr_year = Convert.ToInt32(DateUtil.GetYearEx(0, "yyyy"));

			for (int i = 2014; i <= li_curr_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_pre_year.Items.Add(new ListItem(ls_year, ls_year));
				ddl_search_this_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			//전월
			ddl_search_pre_year.SelectedValue = DateUtil.GetMonthEx(-1, "yyyy");
			ddl_search_pre_month.SelectedValue = DateUtil.GetMonthEx(-1, "MM");

			//당월
			ddl_search_this_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy");
			ddl_search_this_month.SelectedValue = DateUtil.GetMonthEx(0, "MM");

		}

		#endregion

		#region 디테일 Method (SetPrevNextMonth, SetEnabledMonthBtn, SetThisMonth)

		//이전달, 다음달버튼 누르면
		private void SetPrevNextMonth(int pi_month_cnt)
		{
			string ls_ym = ddl_search_this_year.SelectedValue + "-" + ddl_search_this_month.SelectedValue;
			string ls_calc_pre_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt - 1, "yyyy-MM"); //전월
			string ls_calc_this_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt, "yyyy-MM"); //당월

			//전월
			if (WebControlUtil.CheckDdlValue(ddl_search_pre_year, ls_calc_pre_ym.Substring(0, 4).ToString()))
			{
				ddl_search_pre_year.SelectedValue = ls_calc_pre_ym.Substring(0, 4).ToString();
				ddl_search_pre_month.SelectedValue = ls_calc_pre_ym.Substring(5, 2).ToString();
			}
			
			//당월
			if (WebControlUtil.CheckDdlValue(ddl_search_this_year, ls_calc_this_ym.Substring(0, 4).ToString()))
			{
				ddl_search_this_year.SelectedValue = ls_calc_this_ym.Substring(0, 4).ToString();
				ddl_search_this_month.SelectedValue = ls_calc_this_ym.Substring(5, 2).ToString();
			}

			//버튼처리
			SetEnabledMonthBtn();
		}

		//이전달, 다음달버튼 처리
		private void SetEnabledMonthBtn()
		{
			ibtn_prev_month.Enabled
				= (ddl_search_pre_year.SelectedIndex == 0 && ddl_search_pre_month.SelectedIndex == 0) ? false : true;

			ibtn_next_month.Enabled
				= (ddl_search_this_year.SelectedIndex == ddl_search_this_year.Items.Count - 1 && ddl_search_this_month.SelectedIndex == 11) ? false : true;
		}

		//당월설정
		private void SetThisMonth()
		{
			//전월
			ddl_search_pre_year.SelectedValue = DateUtil.GetMonthEx(-1, "yyyy");
			ddl_search_pre_month.SelectedValue = DateUtil.GetMonthEx(-1, "MM");

			//당월
			ddl_search_this_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy");
			ddl_search_this_month.SelectedValue = DateUtil.GetMonthEx(0, "MM");

			SetEnabledMonthBtn();
		}

		#endregion

		#region CRUD (SearchProcess)

		//검색
		private void SearchProcess()
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_pre_month", typeof(string), kw1);
			lo_dp.AddProperty("ls_this_month", typeof(string), kw2);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Fit2013r_NT _Fit2013r_NT = new Fit2013r_NT())
			{
				lo_ds.Merge(_Fit2013r_NT.GetWeightTrainingAnalysisComparedMonth(lo_dp));
			}

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		//Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
					//사용권한별 버튼처리
					PagePreRender.SetVisibleCtrl(this, access.CheckPgmUsingGrant(USER_DIV, PID), access.GetVisibleCtrl(PID));
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			GridView gv = (GridView)sender;

			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//중량차이
				if (Convert.ToDouble(StringUtil.IsNullOrEmptyOrNbsp(e.Row.Cells[6].Text) ? "0" : e.Row.Cells[6].Text) < 0)
				{
					e.Row.Cells[6].Attributes.Add("class", "color_darkred b");
				}

				//달성필요일(중량)
				if (Convert.ToDouble(StringUtil.IsNullOrEmptyOrNbsp(e.Row.Cells[7].Text) ? "0" : e.Row.Cells[7].Text) > 0)
				{
					e.Row.Cells[7].Attributes.Add("class", "color_darkred b");
				}

				//달성비율(중량)
				if (ConversionUtil.ExtractNumberDbl(e.Row.Cells[8].Text) < 100)
				{
					e.Row.Cells[8].Attributes.Add("class", "color_darkred b");
				}

				//횟수차이
				if (Convert.ToDouble(StringUtil.IsNullOrEmptyOrNbsp(e.Row.Cells[11].Text) ? "0" : e.Row.Cells[11].Text) < 0)
				{
					e.Row.Cells[11].Attributes.Add("class", "color_darkred b");
				}

				//달성필요일(횟수)
				if (Convert.ToDouble(StringUtil.IsNullOrEmptyOrNbsp(e.Row.Cells[12].Text) ? "0" : e.Row.Cells[12].Text) > 0)
				{
					e.Row.Cells[12].Attributes.Add("class", "color_darkred b");
				}

				//달성비율(횟수)
				if (ConversionUtil.ExtractNumberDbl(e.Row.Cells[13].Text) < 100)
				{
					e.Row.Cells[13].Attributes.Add("class", "color_darkred b");
				}

				string viewOrder = gv.DataKeys[e.Row.RowIndex].Values["View_Order"].ToString();
				switch (viewOrder)
				{
					case "BA001": //소계
						e.Row.Attributes.Add("class", "sub_total_v1");
						break;
					case "BA002": //총계
						e.Row.Attributes.Add("class", "total_v3");
						for (int i = 0; i < e.Row.Cells.Count; i++)
						{
							e.Row.Cells[i].Attributes.Add("class", "div_line_tb_dbl");
						}
						//총계 colspan
						e.Row.Cells[1].ColumnSpan = 2;
						e.Row.Cells[2].Visible = false;
						e.Row.Cells[3].Visible = false;
						break;
					default:
						e.Row.Cells[6].Style.Add("background-color", "#FFF6D0");
						e.Row.Cells[7].Style.Add("background-color", "#E5F2FF");
						e.Row.Cells[8].Style.Add("background-color", "#D8E3FC");
						e.Row.Cells[11].Style.Add("background-color", "#FFF6D0");
						e.Row.Cells[12].Style.Add("background-color", "#E5F2FF");
						e.Row.Cells[13].Style.Add("background-color", "#D8E3FC");
						break;
				}

				//4,5,9,10에만 링크적용
				for (int i = 4; i <= 10; i++)
				{
					if (i == 4 || i == 5 || i == 9 || i == 10)
					{
						string training_part = gv.DataKeys[e.Row.RowIndex].Values["Training_Part"].ToString();
						string training_type_id = gv.DataKeys[e.Row.RowIndex].Values["Training_Type_Id"].ToString();
						string baseYm = "";

						if (i == 4 || i == 9) //전월
						{
							baseYm = ddl_search_pre_year.SelectedValue + "-" + ddl_search_pre_month.SelectedValue;
						}
						else if (i == 5 || i == 10) //당월
						{
							baseYm = ddl_search_this_year.SelectedValue + "-" + ddl_search_this_month.SelectedValue;
						}

						//소계는 Training_Type_Id를 빈값으로 넘긴다.
						if (training_type_id == "BA001")
						{
							training_type_id = "";
						}

						//총계는 Training_Part와 Training_Type_Id를 빈값으로 넘긴다.
						if (training_type_id == "BA002")
						{
							training_part = "";
							training_type_id = "";
						}

						string data = e.Row.Cells[i].Text;
						string link = (data == "0") ? "javascript:void(0);" : "javascript:goMonthlyWeightTrainingResultPage('" + baseYm + "','" + training_part + "','" + training_type_id + "');";

						string finalData = "<span onclick=\"" + link + "\">" + data + "</span>";
						e.Row.Cells[i].Text = finalData;
					}
				}
			}
			
			SMGridView sGv = new SMGridView(this.Page, ref GridView1);
			SortedList lo_sl = new SortedList();

			//헤더의 rowspan되는 Cell을 삭제함
			if (e.Row.RowType == DataControlRowType.Header)
			{
				e.Row.Cells[1].Visible = false;
				e.Row.Cells[3].Visible = false;
			}

			//멀티헤더설정
			lo_sl.Add("1", global.GetLabelNmBySeq(338, LANG_SET) + ",1,2");	//운동부위
			lo_sl.Add("2", global.GetLabelNmBySeq(336, LANG_SET) + ",1,2");	//운동명
			lo_sl.Add("3", global.GetLabelNmBySeq(458, LANG_SET) + ",5,1");	//중량
			lo_sl.Add("4", global.GetLabelNmBySeq(177, LANG_SET) + ",5,1");	//반복횟수
			sGv.SetMultiHeader(e, lo_sl, "multi_header");

			//머지
			sGv.RowSpanGridView(GridView1, 1); //1.Training_Part_Nm
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledMonthBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_pre_month", typeof(string), kw1);
			lo_dp.AddProperty("ls_this_month", typeof(string), kw2);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Fit2013r_NT _Fit2013r_NT = new Fit2013r_NT())
			{
				lo_ds.Merge(_Fit2013r_NT.GetWeightTrainingAnalysisComparedMonth(lo_dp));
			}

			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		//이전달
		protected void ibtn_prev_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(-1);
			SearchProcess();
		}

		//다음달
		protected void ibtn_next_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(1);
			SearchProcess();
		}

		//당월
		protected void ibtn_this_month_Click(object sender, EventArgs e)
		{
			SetThisMonth();
			SearchProcess();
		}

		#endregion
	}
}